package com.yxw.yxnet_cd_center.customer_service.utils;

import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.enums.CommonErrorEnum;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis管理操作
 * @author Administrator
 */
@Slf4j
@Component
public class RedisManager {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 存入redis 带过期时间
     * @param key 键
     * @param value 值
     * @param time  过期时间，单位秒
     */
    public void set(String key, Object value, long time) {
        try {
            redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("带过期时间存入redis数据异常：{}", e);
            BaseException exception = new BaseException(CommonErrorEnum.REDIS_ERROR);
            exception.setPrimaryErrorCode(CommonConstant.SYSTEM_NAME + CommonConstant.PLUS
                    + CommonErrorEnum.REDIS_ERROR.getCode());
            exception.setPrimaryErrorMsg(CommonErrorEnum.REDIS_ERROR.getDescription());
            exception.setPrimaryErrorIP(IpUtil.getIp());
            throw exception;
        }

    }

    /**
     * 存入redis 不带过期时间
     * @param key 键
     * @param value 值
     */
    public void set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
        } catch (Exception e) {
            log.error("存入redis数据异常：{}", e);
            BaseException exception = new BaseException(CommonErrorEnum.REDIS_ERROR);
            exception.setPrimaryErrorCode(CommonConstant.SYSTEM_NAME + CommonConstant.PLUS
                    + CommonErrorEnum.REDIS_ERROR.getCode());
            exception.setPrimaryErrorMsg(CommonErrorEnum.REDIS_ERROR.getDescription());
            exception.setPrimaryErrorIP(IpUtil.getIp());
            throw exception;
        }
    }

    /**
     * 获取redis存入值
     */
    public Object get(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error("redis获取异常：{}", e);
            BaseException exception = new BaseException(CommonErrorEnum.REDIS_ERROR);
            exception.setPrimaryErrorCode(CommonConstant.SYSTEM_NAME + CommonConstant.PLUS
                    + CommonErrorEnum.REDIS_ERROR.getCode());
            exception.setPrimaryErrorMsg(CommonErrorEnum.REDIS_ERROR.getDescription());
            exception.setPrimaryErrorIP(IpUtil.getIp());
            throw exception;
        }
    }

    /**
     * 删除 key
     * @param key 键
     * @return Boolean
     */
    public Boolean del(String key) {
        try {
            return redisTemplate.delete(key);
        } catch (Exception e) {
            log.error("redis删除key异常：{}", e);
            BaseException exception = new BaseException(CommonErrorEnum.REDIS_ERROR);
            exception.setPrimaryErrorCode(CommonConstant.SYSTEM_NAME + CommonConstant.PLUS
                    + CommonErrorEnum.REDIS_ERROR.getCode());
            exception.setPrimaryErrorMsg(CommonErrorEnum.REDIS_ERROR.getDescription());
            exception.setPrimaryErrorIP(IpUtil.getIp());
            throw exception;
        }
    }
    /**
     * 批量存入redis
     *
     * @param valueMap
     */    
    public void multiSet(Map<String,Object> valueMap)
    {
        /*
        Map valueMap = new HashMap<String,Object>();
        map.forEach((key, value) -> {
            valueMap.put(key, value);
        });  
         */
        try
        {
            redisTemplate.opsForValue().multiSet(valueMap);
        } 
        catch (Exception e) {
            log.error("批量存入redis数据异常：{}", e);
            BaseException exception = new BaseException(CommonErrorEnum.REDIS_ERROR);
            exception.setPrimaryErrorCode(CommonConstant.SYSTEM_NAME + CommonConstant.PLUS
                    + CommonErrorEnum.REDIS_ERROR.getCode());
            exception.setPrimaryErrorMsg(CommonErrorEnum.REDIS_ERROR.getDescription());
            exception.setPrimaryErrorIP(IpUtil.getIp());
            throw exception;
        }        
    }    
}

